package com.bunnyhsu.controller.wxModule;

import com.bunnyhsu.dto.Result;
import com.bunnyhsu.enums.FailEnum;
import com.bunnyhsu.model.Image;
import com.bunnyhsu.service.CommitterService;
import com.bunnyhsu.service.ImageService;
import com.bunnyhsu.service.SuggestService;
import com.bunnyhsu.utils.FileUtil;
import com.bunnyhsu.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * Created by xl on 2018/4/16.
 */
@RestController
@Transactional
@CrossOrigin("*")
public class SuggestWXController {
    @Autowired
    private SuggestService suggestService;

    @Autowired
    private CommitterService committerService;

    @Autowired
    private ImageService imageService;

    @PostMapping("/handInSuggest")
    public Result handInSuggest(String content,String userId)
    {
        try{
            suggestService.addSuggest(content,userId);
            return ResultUtil.success("","提交成功");
        }catch (Exception e){
            return ResultUtil.fail(FailEnum.HANDIN_ERROR.getId(),FailEnum.HANDIN_ERROR.getMsg());
        }
    }

    @PostMapping("/updateCommitter")
    public Result updateCommitter(String id, String image, String nickname)
    {
        committerService.updateCommitter(id,image,nickname);
        return ResultUtil.success("","提交成功");
    }

    @Value("${upload.location}")
    private String location;
    private final String UPLOAD_URL="/upload/images/";

    @PostMapping("/fileUpload")
    @Transactional(rollbackFor = Exception.class)
    public Integer fileUpload(@RequestParam("file")MultipartFile[] file, String content,String userId,
                             @RequestParam(required = false, defaultValue = "0") int postId) throws Exception {

        try {
            if(postId == 0 ){
                int id = suggestService.addSuggest(content,userId);

                for(int i = 0 ; i < file.length ; i++){
                    String fileName = file[i].getOriginalFilename();
                    File path = new File(ResourceUtils.getURL("file:").getPath()
                            + location + UPLOAD_URL);
                    String filePath = path.getAbsolutePath();
                    FileUtil.uploadFile(file[i].getBytes(), filePath, fileName);
                    Image image = new Image();
                    image.setSuggest_id(id);
                    image.setUrl(UPLOAD_URL + fileName);
                    imageService.addImages(image);
                }

            return id;
            }
            else {
                for(int i = 0 ; i < file.length ; i++){
                    String fileName = file[i].getOriginalFilename();
                    File path = new File(ResourceUtils.getURL("file:").getPath()
                            + location + UPLOAD_URL);
                    String filePath = path.getAbsolutePath();
                    FileUtil.uploadFile(file[i].getBytes(), filePath, fileName);
                    Image image = new Image();
                    image.setSuggest_id(postId);
                    image.setUrl(UPLOAD_URL + fileName);
                    imageService.addImages(image);
                }
                return postId;
            }

        } catch (Exception e) {
            throw e;
        }
    }

    @PostMapping("getAllRepliedSuggest")
    public Result getAllRepliedSuggest(String userId)
    {
        return ResultUtil.success(suggestService.getRepliedSuggestByCommitterId(userId));
    }
}
